package com.tintintech.sendmail.protocol;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;

import com.tintintech.sendmail.protocol.MimeMessage;
import com.tintintech.sendmail.util.SmtpServer;

public class SmtpCommand {

	private SmtpServer server;
	private MimeMessage message;
	
	protected OutputStream out;
	protected BufferedReader reader;
	
	public SmtpCommand(SmtpServer server, MimeMessage message) throws ConnectException
	{
		if (server.getSocket().isConnected() == false)
		{
			throw new ConnectException("socket not connection!");
		}
		this.server = server;
		this.message = message;
		
		try {
			this.out = this.server.getSocket().getOutputStream();
			this.reader = new BufferedReader(new InputStreamReader(this.server.getSocket().getInputStream()));
			this.reader.readLine();
			//System.out.println(this.reader.readLine() + " - 1");
		} catch (IOException ioe) {
			// io exception 
			ioe.printStackTrace();
		}
	}
	
	public boolean helo(String to)
	{
		try {
			this.out.write(new String("HELO " + to + "\r\n").getBytes());
			this.out.flush();
			String result = this.reader.readLine();
			if (result != null && result.indexOf("250") != -1)
			{
				return true;
			}
		} catch (IOException ioe){
			ioe.printStackTrace();
		}
		return false;
	}
	
	//EHLO
	public boolean ehlo(String to)
	{
		try
		{
			this.out.write(new String("EHLO" + to + "\r\n").getBytes());
			this.out.flush();
			String result = this.reader.readLine();
			if (result != null && result.indexOf("250") != -1)
			{
				return true;
			}
		}
		catch (IOException ioe)
		{
			return false;
		}
		
		return false;
	}
	
	public boolean mail(String from)
	{
		
		try {
			this.out.write(new String("MAIL FROM: <" + from + ">\r\n").getBytes());
			this.out.flush();
			String result = this.reader.readLine();
			//System.out.println(result + " - 3");
			if (result.indexOf("250") != -1)
			{
				return true;
			}
		} catch (IOException ioe){}	
		return false;
	}
	
	public boolean rcpt(String toemail)
	{
		try {
			this.out.write(new String("RCPT TO:<" + toemail + ">\r\n").getBytes());
			this.out.flush();
			String result = this.reader.readLine();
			//System.out.println(result + " - 4");
			if (result.indexOf("250") != -1)
			{
				return true;
			}
		} catch (IOException ioe){}
		return false;
	}
	
	public boolean data()
	{
		
		try {
			this.out.write("DATA\r\n".getBytes());
			this.out.flush();
			String result  = this.reader.readLine();
			if (result.indexOf("354") != -1)
			{
				//发送信息主体内容
				//System.out.println(result + " - 5");
				//System.out.println(this.message.getMessageBody());
				this.out.write(this.message.getMessageBody().getBytes());
				this.out.write(new String(".\r\n").getBytes());
				this.out.flush();
				result = this.reader.readLine();
				if (result.indexOf("250") != -1)
				{
					return true;
				}
				else
				{
					//System.out.println("send data error!" + result);
                    return false;
				}
			}
			else
			{
				//System.out.println("send data error!");
                return false;
			}
		} catch (IOException ioe){
			//ioe.printStackTrace();
            //record log
		}
		
		return false;
	}
	
	public boolean sendMessage()
	{
		if (this.helo(message.getToEmail()) == false && this.ehlo(message.getToEmail()) == false)
		{
			server.close();
			return false;
		}
		if (this.mail(message.getFromEmail()) == false)
		{
			server.close();
			return false;
		}
		if (this.rcpt(message.getToEmail()) == false)
		{
			server.close();
			return false;
		}
		if (this.data() == false)
		{
			server.close();
			return false;
		}
		this.quit();
		
		return true;
	}
	
	public boolean quit()
	{
		try {
			this.out.write("QUIT\r\n".getBytes());
			this.out.flush();
			if (this.reader.readLine().indexOf("221") != -1)
			{
				return true;
			}
		} catch (IOException ioe){
			System.out.println("smtp-quit-err");
		} finally {
			try {
				this.reader.close();
				this.out.close();
			} catch (Throwable e) {}
			server.close();
		}
		
		return false;
	}
	
}
